package com.autotest.nore.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.autotest.nore.domain.Api;
import com.autotest.nore.mapper.ApiMapper;
import com.autotest.nore.dto.ApiDTO;
import com.autotest.nore.request.ApiRequest;
import com.autotest.nore.request.QueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class ApiService {

    @Autowired
    private ApiMapper apiMapper;

    public void saveApi(ApiRequest apiRequest) {
        JSONObject apiObject = (JSONObject) JSON.toJSON(apiRequest);
        Api api = apiObject.toJavaObject(Api.class);
        if(api.getId().equals("") || api.getId() == null){ // 新增接口
            api.setId(UUID.randomUUID().toString());
            api.setCreateTime(System.currentTimeMillis());
            api.setUpdateTime(System.currentTimeMillis());
            api.setCreateUser(api.getUpdateUser());
            api.setStatus("Normal");
            apiMapper.addApi(api);
        }else{ // 修改接口
            api.setUpdateTime(System.currentTimeMillis());
            apiMapper.updateApi(api);
        }
    }

    public  void copyApi(ApiRequest apiRequest){
        // 1. 根据传入的ID获取原始API信息
        Api originalApi = apiMapper.getApiDetail(apiRequest.getId());
        if (originalApi == null) {
            throw new RuntimeException("要复制的API不存在");
        }
        // 2. 创建新的API对象，复制原始API的属性
        Api newApi = new Api();
        newApi.setId(UUID.randomUUID().toString()); // 生成新ID
        newApi.setName(originalApi.getName() + "_Copy"); // 添加Copy后缀以示区别
        newApi.setLevel(originalApi.getLevel());
        newApi.setModuleId(originalApi.getModuleId());
        newApi.setProjectId(originalApi.getProjectId());
        newApi.setMethod(originalApi.getMethod());
        newApi.setPath(originalApi.getPath());
        newApi.setProtocol(originalApi.getProtocol());
        newApi.setDomainSign(originalApi.getDomainSign());
        newApi.setDescription(originalApi.getDescription());
        newApi.setHeader(originalApi.getHeader());
        newApi.setBody(originalApi.getBody());
        newApi.setQuery(originalApi.getQuery());
        newApi.setRest(originalApi.getRest());
        newApi.setStatus("Normal"); // 设置状态为Normal
        // 3. 设置创建和更新时间
        long currentTime = System.currentTimeMillis();
        newApi.setCreateTime(currentTime);
        newApi.setUpdateTime(currentTime);
        // 4. 设置创建和更新用户
        newApi.setCreateUser(apiRequest.getUpdateUser());
        newApi.setUpdateUser(apiRequest.getUpdateUser());
        // 5. 保存新API
        apiMapper.addApi(newApi);
    }

    public void deleteApi(ApiRequest apiRequest) {
        apiMapper.deleteApi(apiRequest.getId());
    }

    public ApiDTO getApiDetail(String apiId) {
        return apiMapper.getApiDetail(apiId);
    }

    public List<ApiDTO> getApiList(QueryRequest request){
        if(request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition("%"+request.getCondition()+"%");
        }
        return apiMapper.getApiList(request);
    }




}
